//
//  GuideViewController.m
//  Funny
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 King. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *pageScroller;
@property (nonatomic, strong) UIButton *entryClick;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.pageScroller];
    self.isOpen = NO;
    [[NSUserDefaults standardUserDefaults]setObject:@"anyboay" forKey:@"last"];
    [self.view addSubview:self.pageControl];
    for (int i = 1; i < 4; i++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth * (i - 1), 0, ScreenWidth, ScreenHeight)];
        [self.pageScroller addSubview:image];
        UIImage *pic = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        image.image = pic;
    }
    [self.pageScroller addSubview:self.entryClick];
    // Do any additional setup after loading the view.
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(ScreenWidth / 2 - 30, ScreenHeight - 20, 60, 20)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    }
    return _pageControl;
}

- (UIScrollView *)pageScroller
{
    if (!_pageScroller) {
        self.pageScroller = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _pageScroller.delegate = self;
        _pageScroller.pagingEnabled  = YES;
        _pageScroller.contentSize = CGSizeMake(ScreenWidth * 3, 0);
        _pageScroller.showsHorizontalScrollIndicator = NO;
    }
    return _pageScroller;
}

- (UIButton *)entryClick
{
    if (!_entryClick) {
        self.entryClick = [UIButton buttonWithFrame:CGRectMake(ScreenWidth * 2 + ScreenWidth / 2 - 50, ScreenHeight - 60, 100, 30) color:nil title:@"进入应用" type:UIButtonTypeSystem target:self action:@selector(entryClickWithBtn:) event:UIControlEventTouchUpInside cornerRadius:15];
        self.entryClick.layer.masksToBounds = YES;
        self.entryClick.layer.borderWidth = 1;
        self.entryClick.layer.borderColor = [UIColor whiteColor].CGColor;
        self.entryClick.tintColor = [UIColor whiteColor];
    }
    return _entryClick;
}

- (void)entryClickWithBtn:(UIButton *)btn
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"guide" object:nil userInfo:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / ScreenWidth;
    if (self.pageScroller.contentOffset.x > ScreenWidth * 2 + 20) {
        self.isOpen = YES;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.isOpen) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"guide" object:nil userInfo:nil];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
